// Author: Mike Moore
var personPickParamName = null;
var onPersonSelected  = null;
var remoteMethodCommand = null;

function PersonSearchDiv() {
   log('Set elements to null, they will be lazy loaded later');

   this.showPersonSearchDiv = showPersonSearchDiv;
   this.closePersonDiv = closePersonDiv;
   this.findPersonMatches = findPersonMatches;
   onPersonSelected = null;
   //this.contextRoot = null;
}

function setOnPersonSelected(callbackMethod) {
	onPersonSelected = callbackMethod;
}

function selectPersonItem(id, lastName, firstName, middleName) {
	log('Selected Item: ' + id + '  Name: ' + lastName);
	if (onPersonSelected == null) {
		receivePersonPick(id, lastName, firstName, middleName);
	} else {
		onPersonSelected(id, lastName, firstName, middleName);
	}
	closePersonDiv();
}

function getPersonSearchCaption() {
    var searchCaptionId = 'person_search_caption';
    
    var searchCaption = document.getElementById(searchCaptionId);
    if (searchCaption == null) {
    	log('*** ERROR - searchCaption is null - elementId = ' + searchCaptionId);
    }
    return(searchCaption);
}

function getPersonSearchDiv() {
    var searchDivId = 'person_search_div';
    var searchDiv = document.getElementById(searchDivId);
    return(searchDiv);
}

//
function showPersonSearchDiv(remoteMethod, left, top, caption, paramName) {
	log("Displaying search div");
	personPickParamName = paramName;
	remoteMethodCommand = remoteMethod;

	var searchDiv = getPersonSearchDiv();
	searchDiv.style.visibility = 'visible';

	searchDiv.style.height = 400;
	searchDiv.style.position = 'absolute';
	searchDiv.style.bgColor = '#ffffff';
	searchDiv.style.width = 400;
	searchDiv.style.left = left;
	searchDiv.style.top = top;
	searchDiv.style.border = 'solid 1px #000000';
	searchDiv.style.zindex = 5;

	var searchCaption = getPersonSearchCaption();
	searchCaption.innerHTML = caption;
	clearPersonSearch();
	
	callFindPersonMatches();
	
}



function closePersonDiv() {
	log('closePersonDiv()');
	var searchDiv = getPersonSearchDiv();
	log('Before closing, div id = ' + searchDiv.id);
	log('Before closing, visibility = ' + searchDiv.style.visibility);
	searchDiv.style.visibility = 'hidden';
	searchDiv.style.display = '';
	log('finished closePersonDiv() - display = ' + searchDiv.style.display );
	
}


/*
function closePersonDiv() {
	log('closePersonDiv()');
	if (this.searchDiv == null) {
	    acquirePersonElements();
	}
	log('Before closing, div id = ' + this.searchDiv.id);
	log('Before closing, visibility = ' + this.searchDiv.style.visibility);
	this.searchDiv.style.visibility = 'hidden';
	this.searchDiv.style.display = '';
	log('finished closePersonDiv() - display = ' + this.searchDiv.style.display );
	
}
*/



 function clearPersonSearch() {
	var lastNameEdit = document.getElementById("edit_lastName");
	lastNameEdit.value = '';

	var firstNameEdit = document.getElementById("edit_firstName");
	firstNameEdit.value = '';

	var phoneEdit = document.getElementById("edit_phoneNumber");
	phoneEdit.value = '';

	var cityEdit = document.getElementById("edit_city");
	cityEdit.value = '';

	var stateEdit = document.getElementById("edit_state");
	stateEdit.value = '';

	document.getElementById("person_search_results").innerHTML = '';
	callFindPersonMatches();
 }
 

 function findPersonMatches(contextRoot, user, domainId, pcode) {
	log('findPersonMatches() called - context = ' + contextRoot);
	var lastNameEdit = document.getElementById("edit_lastName");
	var lastName = lastNameEdit.value;

	var firstNameEdit = document.getElementById("edit_firstName");
	var firstName = firstNameEdit.value;

	var phoneEdit = document.getElementById("edit_phoneNumber");
	var phoneNumber = phoneEdit.value;

	var cityEdit = document.getElementById("edit_city");
	var city = cityEdit.value;

	var stateEdit = document.getElementById("edit_state");
	var state = stateEdit.value;

	log('findPersonMatches() - last name: ' + lastName);
	var href = contextRoot + '/ws?command='+remoteMethodCommand+'&user='
	     + user +  '&domainId=' + domainId + '&pcode=' + pcode + '&lastName=';
	log('findPersonMatches() - href 1: ' + href);
	href += lastName + '&firstName=' + firstName
		+ '&phoneNumber=' + phoneNumber
		+ '&city=' + city
		+ '&state=' + state;
	log('findPersonMatches() - href: ' + href);
	remoteCall(href, handleXmlPersonSearchResponse, personRemoteCallError);
 }
 
 
 // Callback for errors during remote calls.
 function personRemoteCallError(href) {
 	alert('An error occurred attempting to contact the remote server.  Data call: ' + href);
 }


function handleXmlPersonSearchResponse() {
  // if the readyState code is 4 (Completed)
  // and http status is 200 (OK) we go ahead and get the responseText
  // other readyState codes:
  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
  log("Response received from server, processing XML response.");
  var xmlhttp = getXmlHttp();
  if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
				xmlDoc =
					AjxXmlDoc.createFromXml(xmlhttp.responseText);

				nativeDocument = xmlDoc.getDoc();
				currentPersonNodes = nativeDocument.getElementsByTagName("person");
				displayPersonNodes(currentPersonNodes);
		} else {
			log("Not processed - Response status = " + xmlhttp.status);
		}
  } else {
		log("Not processed - Response readyState = " + xmlhttp.readyState);
  }
}


function displayPersonNodes(currentPersonNodes) {
	var normalRowColor = '#ffffff';
	var selectRowColor = '#f8f8f8';
	var quotedNormalRowColor = "'" + normalRowColor + "'";
	var quotedSelectRowColor = "'" + selectRowColor + "'";

	var resultsTable = '<table width="550" style="cursor: pointer">';
	resultsTable += '<tr>'
		+ '<td><div class="subFont"><b>Id</b></div></td>'
		+ '<td><div class="subFont"><b>Last Name</b></font></td>'
		+ '<td><div class="subFont"><b>First Name</b></font></td>'
		+ '<td><div class="subFont"><b>Home Phone</b></font></td>'
		+ '<td><div class="subFont"><b>City</b></font></td>'
		+ '<td><div class="subFont"><b>State</b></font></td>'
		+ '</tr>';
	for (i = 0; i < currentPersonNodes.length; i++) {
		node = currentPersonNodes[i];
		var quotedId = "'" + node.getAttribute("id") + "'";
		var quotedLastName = "'" + node.getAttribute("lastName") + "'";
		var quotedFirstName = "'" + node.getAttribute("firstName") + "'";
		var quotedMiddleName = "'" + node.getAttribute("middleName") + "'";
		var trStr;
		trStr = '<tr'
		trStr += ' bgcolor='  + quotedNormalRowColor;
		trStr += ' onClick="selectPersonItem(' + quotedId + ', ' + quotedLastName + ', ' + quotedFirstName + ', ' + quotedMiddleName + ');"';
		trStr += ' onMouseOver="changeColor(this, '	+ quotedSelectRowColor + ');"';
		trStr += ' onMouseOut="changeColor(this, '	+ quotedNormalRowColor + ');"';
		trStr += '>';
		//log(trStr);

		var homePhone = node.getAttribute("homePhone");
		if( homePhone == null ) {
			homePhone = '';
		}
		
		var detailStr = trStr
			+ '<td><div class="subFont">' + node.getAttribute("id") + '</div></td>'
			+ '<td><div class="subFont">' + node.getAttribute("lastName") + '</div></td>'
			+ '<td><div class="subFont">' + node.getAttribute("firstName") + '</div></td>'
			+ '<td><div class="subFont">' + homePhone + '</div></td>'
			+ '<td><div class="subFont">' + node.getAttribute("city") + '</div></td>'
			+ '<td><div class="subFont">' + node.getAttribute("state") + '</div></td>'
			+ '</tr>';
		resultsTable += detailStr;
	}
	resultsTable += '</table>';
	log('Setting person table to new contents');
	document.getElementById("person_search_results").innerHTML = resultsTable;
	log('Done - Setting person table to new contents');
}


//Method to receive person pick dialog selection
function receivePersonPick(personId, lastName, firstName, middleName) {
    var paramName = personPickParamName;
    var inputId = 'person_picker_' + paramName;
    log('Finding DIV that displays person name - element id = ' + inputId);
    var personDiv = document.getElementById(inputId);
    personDiv.innerHTML = '<div class="stdFont">' + firstName + '&nbsp;' + lastName +  '</div>';
    var hidden = document.getElementById(paramName);
    hidden.value = personId;
};
